package dvonn.game;

public class Place {
	
	private Token token;
	private byte size;
	private boolean dvonn;
	private boolean movable;

	/**
	 * inits an empty place
	 */
	public Place() {
		this.token = Token.EMPTY;
		this.size = 0;
		this.dvonn = false;
		this.movable = false;
	}
	
	/**
	 * init a new place equal to the given 
	 * @param p an other Place
	 */
	public Place(Place p) {
		this.token = p.getToken();
		this.size = p.getSize();
		this.dvonn = p.withDvonn();
		this.movable = p.movable;
	}
	
	/**
	 * 
	 * @return number of token on a place
	 */
	public byte getSize() {
		return this.size;
	}
	
	/**
	 * 
	 * @return type of token on top of a place
	 */
	public Token getToken() {
		return this.token;
	}
	
	/**
	 * 
	 * @return true if a dvonn token is inside the tower on this place
	 */
	public boolean withDvonn() {
		return this.dvonn;
	}
	
	/**
	 * 
	 * @return true if this place is empty
	 */
	public boolean isEmpty() {
		return this.token == Token.EMPTY;
	}
	
	/**
	 * 
	 * @return true if this place is reserved and not for playing
	 */
	public boolean isReserved() {
		return this.token == Token.RESERVED;
	}

	/**
	 * 
	 * @return true if token on this place is movable
	 */
	public boolean isMovable() {
		return this.movable;
	}
	
	/**
	 * 
	 * @param t the token to put on this place
	 * @return true if able to put this token  
	 */
	public boolean setToken(Token t) {
		if (this.token != Token.EMPTY) return false;
		this.token = t;
		if ((t != Token.EMPTY)&&(t != Token.RESERVED)) this.size = 1;
		if (t == Token.DVONN) {
			this.dvonn = true;
			//this.movable = false;
		}
		return true;
	}
	
	/**
	 * empty this place
	 */
	public void clear() {
		if (this.token != Token.RESERVED) {
			this.token = Token.EMPTY;
			this.dvonn = false;
			//this.movable = false;
			this.size = 0;
		}
	}
	
	/**
	 * set the token on this place as movable if not reserved
	 */
	public void setMovable() {
		if ( this.token != Token.RESERVED ) this.movable = true;
	}
	
	/**
	 * 
	 * @param p a other place from which tokens will be added
	 * @return true if able to add
	 */
	public boolean add(Place p) {
		if ((this.token == Token.RESERVED)||(this.token == Token.EMPTY)) return false;
		if (!p.getToken().isPlayerToken()) return false;
		this.token = p.getToken();
		this.size = (byte) (this.size + p.getSize());
		this.dvonn = this.dvonn || p.withDvonn();
		return true;
	}
	
	public String toString() {
		String output = new String();
		output = "<"+this.token+this.size;
		if (this.dvonn) output = output+"+";
			else output = output+"-";
		if (this.movable) output = output+"m";
		else output = output+"_";
		return output+">";
	}
	
	public boolean equals(Place p) {
		if (this.token!=p.getToken()) return false;
		if (this.dvonn!=p.withDvonn()) return false;
		if (this.size!=p.getSize()) return false;
		if (this.movable!=p.isMovable()) return false;
		return true;
	}
	
}
